/*
  listview

  其他滚动组件:

  ListWheelScrollView
  具有立体效果

  PageView
  分页滚动

  ReorderableListView
  支持拖拉排序

  SingleChildScrollView
  包裹Colum,使其可以滚动,主要用于适配,而不是作为滚动组件,不能包裹过多内容
 */
import 'package:flutter/material.dart';

void main() {
  runApp(ListView3());
}

/*
  基本用法，一般不这么用
 */
class ListView1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'listView',
      home: Scaffold(
        appBar: AppBar(title: Text('ListView')),
        body: ListView(
          itemExtent: 100, // 指定每个item的固定高度，横向列表则为宽度，可以提高滑动效率
          shrinkWrap: true, // 默认是充满父控件，shrinkWrap则是包裹子控件
          physics:
              NeverScrollableScrollPhysics(), // 当滚动到边界时的效果，IOS上是拉伸效果，Android则是不滑动
          padding: const EdgeInsets.all(16.0), // 内边距
          children: [
            Container(
              height: 50,
              color: Colors.amber[100],
              child: const Center(
                child: Text('Text A'),
              ),
            ),
            Container(
              height: 50,
              color: Colors.amber[200],
              child: const Center(
                child: Text('Text B'),
              ),
            ),
            Container(
              height: 50,
              color: Colors.amber[300],
              child: const Center(
                child: Text('Text C'),
              ),
            )
          ],
        ),
      ),
    );
  }
}

/*
  日常用法
 */
class ListView2 extends StatelessWidget {
  final List<String> entries = <String>['A', 'B', 'C'];
  final List<int> colorCodes = <int>[600, 500, 100];
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'listView',
      home: Scaffold(
          appBar: AppBar(title: Text('ListView')),
          body: ListView.builder(
              itemCount: 10, // 如果不写则无线个数
              itemBuilder: (BuildContext context, int index) {
                // 滑动删除操作
                return Dismissible(
                  key: UniqueKey(),
                  onDismissed: (direction) {
                    // 回调函数里一定要删除对应的数据
                    Future.delayed(Duration(seconds: 2));
                  },
                  child: Container(
                    height: 50,
                    color: Colors.amber[colorCodes[index % 3]],
                    child: Center(child: Text('Entry ${entries[index % 3]}')),
                  ),
                );
              })),
    );
  }
}

/*
  带分割线用法
 */
class ListView3 extends StatefulWidget {
  @override
  State<ListView3> createState() => _ListView3State();
}

class _ListView3State extends State<ListView3> {
  final List<String> entries = <String>['A', 'B', 'C'];

  final List<int> colorCodes = <int>[600, 500, 100];

  late final ScrollController? _controller;

  @override
  void initState() {
    super.initState();
    _controller = ScrollController();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'listView',
      home: Scaffold(
        appBar: AppBar(title: Text('ListView')),
        // 下拉刷新指示器
        body: RefreshIndicator(
          onRefresh: () => Future.delayed(Duration(seconds: 2)),
          child: ListView.separated(
              controller: _controller,
              separatorBuilder: (BuildContext context, int index) =>
                  const Divider(),
              itemCount: 10, // 如果不写则无线个数
              itemBuilder: (BuildContext context, int index) {
                return Container(
                  height: 50,
                  color: Colors.amber[colorCodes[index % 3]],
                  child: Center(child: Text('Entry ${entries[index % 3]}')),
                );
              }),
        ),
        floatingActionButton: TextButton(
          onPressed: () {
            _controller!.jumpTo(0); // 跳转到位置
            _controller!.animateTo(300,
                duration: Duration(seconds: 1), curve: Curves.linear); // 通过动画
          },
          child: Icon(Icons.arrow_drop_down),
        ),
      ),
    );
  }
}
